package biz.datalk.industrialland.admin.modules.online.dynform.service.impl;

import biz.datalk.commons.utils.IdProvider;
import biz.datalk.industrialland.admin.modules.online.dynform.mapper.IDynformEnhanceJsMapper;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.dto.DynformEnhanceJsDTO;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformEnhanceJs;
import biz.datalk.industrialland.admin.modules.online.dynform.pojo.po.DynformEnhanceJsExample;
import biz.datalk.industrialland.admin.modules.online.dynform.service.IDynformEnhanceJsService;
import biz.datalk.industrialland.common.exception.ServiceException;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.util.I18NUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author tarofang@163.com
 * @date 2020-01-06
 *
 */
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Slf4j
@Service
public class DynformEnhanceJsServiceImpl implements IDynformEnhanceJsService {

    private final IDynformEnhanceJsMapper dynformEnhanceJsMapper;

    @Autowired
    public DynformEnhanceJsServiceImpl(IDynformEnhanceJsMapper dynformEnhanceJsMapper) {
        this.dynformEnhanceJsMapper = dynformEnhanceJsMapper;
    }


    @Override
    public UnifyResult olist(DynformEnhanceJsDTO dynformEnhanceJsDTO) {
        return result(getSpecialTypeData(dynformEnhanceJsDTO.getTableId(), "list"));
    }

    @Override
    public UnifyResult oform(DynformEnhanceJsDTO dynformEnhanceJsDTO) {
        return result(getSpecialTypeData(dynformEnhanceJsDTO.getTableId(), "form"));
    }

    private DynformEnhanceJs getSpecialTypeData(String tableId, String jsType) {
        List<DynformEnhanceJs> list = selectByTableIdAndJsType(tableId, jsType);
        if (CollectionUtils.size(list) > 1) {
            throw new ServiceException(I18NUtil.getMsg("message.dynform.enhancejs.multiple.data"));
        }
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

    private UnifyResult result(DynformEnhanceJs enhanceJs) {
        if (enhanceJs == null) {
            return UnifyResult.success().mapData();
        }
        return UnifyResult.success().mapData().put("id", enhanceJs.getId()).put("jsType", enhanceJs.getJsType()).put("js", enhanceJs.getJs());
    }

    @Override
    public UnifyResult save(DynformEnhanceJsDTO dynformEnhanceJsDTO) {
        DynformEnhanceJs exists = getSpecialTypeData(dynformEnhanceJsDTO.getTableId(), dynformEnhanceJsDTO.getJsType());
        if (exists == null) {
            dynformEnhanceJsDTO.setId(IdProvider.createUUIDId());
            dynformEnhanceJsMapper.insertSelective(dynformEnhanceJsDTO);
        } else {
            // if (!StringUtils.equals(dynformEnhanceJsDTO.getId(), exists.getId())) {
            //     return UnifyResult.fail(I18NUtil.getMsg("message.dynform.enhancejs.invalid.data"));
            // }
            exists.setJs(dynformEnhanceJsDTO.getJs());
            dynformEnhanceJsMapper.updateByPrimaryKeyWithBLOBs(exists);
        }

        return UnifyResult.success();
    }

    @Override
    public List<DynformEnhanceJsDTO> selectJsByTableId(String tableId) {
        if (StringUtils.isBlank(tableId)) {
            return new ArrayList<>();
        }
        DynformEnhanceJsExample example = new DynformEnhanceJsExample();
        example.createCriteria().andTableIdEqualTo(tableId.trim());
        List<DynformEnhanceJs> jsList = dynformEnhanceJsMapper.selectByExampleWithBLOBs(example);
        jsList = CollectionUtils.isNotEmpty(jsList) ? jsList : new ArrayList<>();

        List<DynformEnhanceJsDTO> resList = new ArrayList<>();
        DynformEnhanceJsDTO dto;
        for (DynformEnhanceJs js : jsList) {
            dto = new DynformEnhanceJsDTO();
            BeanUtils.copyProperties(js, dto);
            resList.add(dto);
        }

        return resList;
    }


    @Override
    public List<DynformEnhanceJs> selectByTableIdAndJsType(String tableId, String jsType) {
        if (StringUtils.isBlank(tableId) || StringUtils.isBlank(jsType)) {
            return new ArrayList<>();
        }
        DynformEnhanceJsExample example = new DynformEnhanceJsExample();
        example.createCriteria().andTableIdEqualTo(tableId).andJsTypeEqualTo(jsType);

        return dynformEnhanceJsMapper.selectByExampleWithBLOBs(example);
    }
}
